#include "uart_app.h"

uint8_t rx_dat[30];
typedef struct
{
    char car[4];
    char time[12];
} car_data;

car_data car_num[10] = {0};

uint8_t uart_finish_flag;
uint8_t uart_num;
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
    uart_finish_flag = 1;
    uart_num = Size;
}

float dollar;   
uint16_t timer; 
uint8_t flag[2]={3,3};//0N,1V
void UART_proc(void)
{
    if (uart_finish_flag)
    {
        uart_finish_flag = 0;
        if (uart_num == 22)
        {
            uart_num = 0;
            if ((rx_dat[4] == ':') && (rx_dat[9] == ':'))
            {

                if ((rx_dat[0] == 'C') && (rx_dat[1] == 'N') && (rx_dat[2] == 'B') && (rx_dat[3] == 'R'))
                {

                    for (int i = 0; i < 10; i++)
                    {
                        if ((rx_dat[5] == car_num[i].car[0]) && (rx_dat[6] == car_num[i].car[1]) && (rx_dat[7] == car_num[i].car[2]) && (rx_dat[8] == car_num[i].car[3]))
                        {


                            timer = ((car_num[i].time[0] - rx_dat[10] + car_num[i].time[1] - rx_dat[11]) * 8760 +
                                     (car_num[i].time[2] - rx_dat[12] + car_num[i].time[3] - rx_dat[13]) * 720 +
                                     ((rx_dat[14] - car_num[i].time[4]) * 10 +  rx_dat[15]- car_num[i].time[5]) * 24 +
                                     ((rx_dat[16] - car_num[i].time[6]) * 10 + rx_dat[17] - car_num[i].time[7]) * 1);
                            if ((rx_dat[18] != '0') || (rx_dat[19] != '0') || (rx_dat[20] != '0') || (rx_dat[21] != '0'))
                                timer += 1;

                            dollar = timer * charge_rate[0];
                            printf("%c%c%c%c:%c%c%c%c:%d:%.2f", 'C', 'N', 'B', 'R', car_num[i].car[0], car_num[i].car[1], car_num[i].car[2], car_num[i].car[3], timer, dollar);
                            flag[0] = 1;
                            Car_state[0]--;
                            Car_state[2]++;
                            for (int j = 0; j < 4; j++)
                                car_num[i].car[j] = 0;
                            break;
                        }
                        else
                        {
                            flag[0] = 0;
                        }
                    }
                }
                else if ((rx_dat[0] == 'V') && (rx_dat[1] == 'N') && (rx_dat[2] == 'B') && (rx_dat[3] == 'R'))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        if ((rx_dat[5] == car_num[i].car[0]) && (rx_dat[6] == car_num[i].car[1]) && (rx_dat[7] == car_num[i].car[2]) && (rx_dat[8] == car_num[i].car[3]))
                        {


                            timer = ((car_num[i].time[0] - rx_dat[10] + car_num[i].time[1] - rx_dat[11]) * 8760 +
                                     (car_num[i].time[2] - rx_dat[12] + car_num[i].time[3] - rx_dat[13]) * 720 +
                                     ((rx_dat[14] - car_num[i].time[4]) * 10 + rx_dat[15] - car_num[i].time[5]) * 24 +
                                     ((rx_dat[16] - car_num[i].time[6]) * 10 + rx_dat[17] - car_num[i].time[7]) * 1);
                            if ((rx_dat[18] != '0') || (rx_dat[19] != '0') || (rx_dat[20] != '0') || (rx_dat[21] != '0'))
                                timer += 1;

                            dollar = timer * charge_rate[0];
                            printf("%c%c%c%c:%c%c%c%c:%d:%.2f", 'V', 'N', 'B', 'R', car_num[i].car[0], car_num[i].car[1], car_num[i].car[2], car_num[i].car[3], timer, dollar);
                            Car_state[1]--;
                            Car_state[2]++;
                            flag[1] = 1;
                            for (int j = 0; j < 4; j++)
                                car_num[i].car[j] = 0;
                            break;
                        }
                        else
                        {
                            flag[1] = 0;
                        }
                    }
                }
            }
            else
            {
                printf("Error");
                return;
            }

            if ((flag[0] == 0) || (flag[1] == 0))
            {
                for (int j = 0; j < 10; j++)
                {
                    if (car_num[j].car[0] == 0)
                    {
                        for (int k = 0; k < 12; k++)
                        {
                            car_num[j].time[k] = rx_dat[10 + k];
                        }
                        for (int l = 0; l < 4; l++)
                            car_num[j].car[l] = rx_dat[5 + l];
                        break;
                    }
                }
                if (flag[0] == 0)
                {
                    Car_state[0]++;
                    Car_state[2]--;
                }
                else
                {
                    Car_state[1]++;
                    Car_state[2]--;
                }
                flag[0] = flag[1] = 3;
            }

        }
        else
        {
            printf("Error\n");
            return;
        }
        
    }
}